package com.sykj.why.controller;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.SystemMenuT;
import com.sykj.why.mongo.document.entity.UserT;
import com.sykj.why.mongo.dto.entity.UpdatePwdDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.SystemMenuTService;
import com.sykj.why.mongo.service.UserTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/5
 * @desc 用户控制访问层
 */

@RestController
@RequestMapping("/user")
@Slf4j
@Api(value = "/user",description = "用户控制访问层")
public class UserTController {

    @Autowired
    private UserTService userTService;

    @Autowired
    private SystemMenuTService systemMenuTService;

    /**
     * 用户登录
     * @param imageCode
     * @param user
     * @param bindingResult
     * @param session
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "/login",notes = "用户登录")
    public ResponseEntity login(@RequestParam String imageCode, @RequestBody UserT user, BindingResult bindingResult, HttpSession session){
        if(StringUtils.isEmpty(imageCode)){
            return Response.serverError("请输入验证码！",10031);
        }
        if(!session.getAttribute("checkcode").equals(imageCode)){
            return Response.serverError("验证码错误！",10032);
        }
        if(bindingResult.hasErrors()){
            return Response.serverError(bindingResult.getFieldError().getDefaultMessage(),10033);
        }
        try {
            UserT userT = userTService.getUser(user);
            if (userT != null){
                userT.setPassWord(user.getPassWord());
                session.setAttribute("currentUser", userT);
                session.setAttribute("currentRole",userT.getRole());
                List<SystemMenuT> systemMenuList = systemMenuTService.getSystemMenuListByPid("1");
                session.setAttribute("tmenuOneClassList", systemMenuList);
                return Response.success("登录成功");
            }else {
                throw new WhyException(ResultEnum.USER_NOT_EXIST);
            }
        }catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("用户登录出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    @RequestMapping(value = "/updatePassWord",method = RequestMethod.POST)
    public ResponseEntity updatePassWord(@RequestParam String id,@RequestParam String passWord){
        try {
            userTService.updatePassWord(id,passWord);
            return Response.success("更改密码成功");
        }catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改密码出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 安全登出
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ApiOperation(value = "/logout",notes = "安全登出")
    public ResponseEntity logout(HttpSession session){
        session.removeAttribute("currentUser");
        return Response.success("登出成功");
    }

    /**
     * 获取系统菜单树状结构
     * @param session
     * @param pid
     * @return
     */
    @RequestMapping(value = "/loadMenuInfo",method = RequestMethod.GET)
    @ApiOperation(value = "/loadMenuInfo",notes = "获取系统菜单树状结构")
    public ResponseEntity loadMenuInfo(HttpSession session,@RequestParam String pid){
        //session保存一级菜单列表
        List<SystemMenuT> systemMenuList = systemMenuTService.getSystemMenuListByPid("1");
        session.setAttribute("tmenuOneClassList", systemMenuList);

        try {
            //根据当前父节点id查询所有菜单及子集json
            String json=systemMenuTService.getAllSystemMenuTreeByPid(pid).toString();
            return Response.success("获取系统菜单列表成功",json);
        }catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("获取系统菜单列表出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
